<?php  
class ControllerModuleCategory extends Controller {
	protected function index($setting) {
		if (isset($setting['showTitle'])){
			$this->data['showTitle'] = $setting['showTitle'];
		}else{
			$this->data['showTitle'] = 0;//show
		}
		if (isset($setting['showType'])){
			$this->data['showType'] = $setting['showType'];
		}else{
			$this->data['showType'] = 0;//single
		}
		
		$this->language->load('module/category');
		$this->load->model('catalog/category');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
		if (isset($this->request->get['keyword'])) {
			$keyword = $this->request->get['keyword'];
			$keyword = rtrim($keyword, '_');
		}else{
			$keyword = '';
		}
		
    	$route = '';
    	$urlName = '';
		if(isset($this->request->get['route'])){
    		$route_r = $this->request->get['route'];
    		if($route_r == ROUTE_PROMOTION){
    			$route = ROUTE_PROMOTION_R;
    		}else if($route_r == ROUTE_DAILY){
    			$route = ROUTE_DAILY_R;
    		}else if($route_r == ROUTE_NEW){
    			$route = ROUTE_NEW_R;
    		}else if($route_r == ROUTE_BESTSELL){
    			$route = ROUTE_BESTSELL_R;
    		}else if($route_r == ROUTE_SEARCH){
    			$route = ROUTE_SEARCH_R;
    			$urlName = $keyword;
    		}else if($route_r == ROUTE_REVIEWS){
    			$route = ROUTE_REVIEWS_R;
    		}
    	}

		if (isset($this->request->get['category_id'])) {
			$categoryIds = $this->request->get['category_id'];
			$parts = explode('_', (string)$categoryIds);
		} else {
			$parts = array();
		}
		
		$pl = count($parts);
		$categoryId = '';
		if($pl == 0){
			$categories = $this->model_catalog_category->getCategories();
		}else if($pl == 1){
			$categoryId = $parts[0] . '_';
			$categories = $this->model_catalog_category->getCategories($categoryId);
			if(!$categories){
				$categoryId = '';
				$categories = $this->model_catalog_category->getCategories();
			}
		}else if($pl > 1){
			$categories = $this->model_catalog_category->getCategories($parts[$pl - 1]);
			if($categories){
				$categoryId = $parts[0] . '_';
				for($i = 1; $i < $pl; $i++){
					$categoryId .= $parts[$i] . '_';
				}
			}else{
				$categoryId = $parts[0] . '_';
				for($i = 1; $i < $pl-1; $i++){
					$categoryId .= $parts[$i] . '_';
				}
				$categories = $this->model_catalog_category->getCategories($parts[$pl - 2]);
				
			}
		}
		
		$this->data['categories'] = array();
		foreach ($categories as $category) {
			if($urlName){
				$urlName_t = $urlName;
			}else{
				$urlName_t = $category['name'];
			}
			$this->data['categories'][] = array(
				'name'        => $category['name'],
				'href'        => $this->url->linkRewrite($route, $urlName_t . '_c' . $categoryId . $category['category_id'])
			);
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
		} else {
			$this->template = 'default/template/module/category.tpl';
		}
		
		$this->render();
  	}
}
?>